package br.com.fiap.fiappizzas.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import br.com.fiap.fiappizzas.db.entities.Order;
import br.com.fiap.fiappizzas.db.entities.OrderProduct;
import br.com.fiap.fiappizzas.db.entities.Product;

public class OrderDAO extends AbstractDAO<Order> {
	
	private OrderProductDAO orderProductDAO;

	public OrderDAO(Context context) {
		super(context);
		orderProductDAO = new OrderProductDAO(context);
	}

	@Override
	public String getTableName() {
		return "tb_order";
	}

	@Override
	public String[] getColumns() {
		return new String[]{"id", "product", "client"};
	}

	@Override
	public Order buildEntity(Cursor cursor) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ContentValues buildContentValues(Order entity) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Order insert(Order entity) {
		
		super.insert(entity);
		
		for (Product p : entity.getProducts()) {
			OrderProduct op = new OrderProduct();
			op.setOrderId(entity.getId());
			op.setProductId(p.getId());
			orderProductDAO.insert(op);
		}
		
		return entity;
	}
	
	@Override
	public void delete(Order entity) {
		
		for (Product p : entity.getProducts()) {
			orderProductDAO.delete(entity.getId(), p.getId());
		}
		
		super.delete(entity);
	}

}
